package com.gcc.service.impl;

import com.gcc.dao.BookDao;
import com.gcc.dao.impl.BookDaoImpl;
import com.gcc.pojo.Book;
import com.gcc.pojo.Page;
import com.gcc.service.BookService;

import java.util.List;

public class BookServiceImpl implements BookService {
    private BookDao bookDao=new BookDaoImpl();
    @Override
    public void addBook(Book book) {
          bookDao.addBook(book);
    }

    @Override
    public void deleteBook(Integer id) {
        bookDao.deleteBookById(id);
    }

    @Override
    public void updateBook(Book book) {
        bookDao.updateBook(book);
    }

    @Override
    public Book queryBookById(Integer id) {
        return bookDao.queryBookById(id);
    }

    @Override
    public List<Book> queryBooks() {
        return bookDao.queryBooks();
    }

    @Override
    public Page<Book> page(int pageNo, int pageSize) {
        Page<Book> page=new Page<>();
        page.setPageSize(pageSize);
        //求总记录数并设置总记录数
        Integer pageTotalCount=bookDao.queryForPageTotalCount();
        page.setPageTotalCout(pageTotalCount);
        //求总页码并且设置总页码
        Integer pageTotal=pageTotalCount%pageSize==0?(pageTotalCount/pageSize):(pageTotalCount/pageSize)+1;
        page.setPageTotal(pageTotal);


        page.setPageNo(pageNo);
        //求当前页数据的开始索引
        int begin=(page.getPageNo()-1)*pageSize;
        //求当前页数据并设置
        List<Book> items=bookDao.queryForPageItems(begin,pageSize);
        page.setItems(items);
        return page;
    }

    @Override
    public Page<Book> pageByPrice(int pageNo, int pageSize, int min, int max) {
        Page<Book> page=new Page<>();
        page.setPageSize(pageSize);
        //求总记录数并设置总记录数
        Integer pageTotalCount=bookDao.queryForPageTotalCountByPrice(min,max);
        page.setPageTotalCout(pageTotalCount);
        //求总页码并且设置总页码
        Integer pageTotal=pageTotalCount%pageSize==0?(pageTotalCount/pageSize):(pageTotalCount/pageSize)+1;
        page.setPageTotal(pageTotal);


        page.setPageNo(pageNo);
        //求当前页数据的开始索引
        int begin=(page.getPageNo()-1)*pageSize;
        //求当前页数据并设置
        List<Book> items=bookDao.queryForPageItemsByPrice(begin,pageSize,min,max);
        page.setItems(items);
        return page;
    }
}
